/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { ReactElement, JSXElementConstructor } from 'react';
import { createFromIconfontCN } from '@ant-design/icons';
import type { IconFontProps } from '@ant-design/icons/lib/components/IconFont';
import { iconFont } from '@base/config';

interface IconFontConfig {
  scriptUrl: string;
  icons: {
    [key: string]: ReactElement<any, string | JSXElementConstructor<any>>;
  };
}

// 框架内图标
const IconsMap = {
  ...iconFont.icons,
};

// 字体图标 结合www.iconfont.cn网站使用
const IconFont = createFromIconfontCN({ scriptUrl: iconFont.scriptUrl });

const IconFontExtend: React.FC<IconFontProps> = (props) => {
  const { type, ...rest } = props;
  const defaultIcon = IconsMap[type];

  return defaultIcon ? (
    React.cloneElement(defaultIcon, { ...rest })
  ) : (
    <IconFont type={type} {...rest} />
  );
};

export { IconFontConfig };
export default IconFontExtend;
